<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Consult;
use App\Models\User;
use App\Models\UserExpert;
use App\Models\WxuserInfo;
use App\Repos\ConsultReplyRepo;

class ConsultController extends Controller
{
    public function index(Request $request)
    {
        $param = $where = [];
        $query = Consult::query();

        if($request->expert_id) {
            $param['expert_id'] = $where['expert_id'] = (int)$request->expert_id;
            $query->where('expert_id',$request->expert_id);
        }

        if(session('admin')->role_id==1) {
            $user_id = session('admin')->id;
            $expert = UserExpert::where(['user_id'=>$user_id])->first();
            $query->where('expert_id',$expert->id);
        }

        

        if($request->keyword) {
            $query->where('title','like','%'.$request->keyword.'%');
            $param['keyword'] = $request->keyword;
        }
        $list  = $query->orderBy('created_at','desc')->paginate(20); 

        // 待回复
        $where['status'] = 0;
        $wait    = Consult::where($where)->count();
       
        //已回复
        $where['status'] = 1;
        $done    = Consult::where($where)->count();


       
        // 总咨询量
        array_pull($where,'status');
        $tasks_total = Consult::where($where)->count();   
        
        return view('admin.consult.index',compact('list','param','done','wait','tasks_total'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
       
        $rs   = Consult::create($data);
        if($rs) { 
           
            return redirect('zadmin/worker');
        }
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = Consult::find($id);
        
        $wxuser = WxuserInfo::where(['openid'=>$data->wxuser_openid])->first();
        return view('admin.consult.show',compact('data','wxuser'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = User::find($id);
        $worktypes = Worktype::get();
        $departs = DepartRepo::get();
        return view('admin.worker.edit',compact('data','worktypes','departs'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
       
        $rs   = User::where('id',$id)->update($data);
        if($rs) {
            return redirect('zadmin/worker');
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs = User::destroy($id);
        if ($rs) {
            return redirect('zadmin/worker');
        }
        return back();
    }

    public function saveReply(Request $request)
    {
        $data = $request->except(['_token']);
        $flag = ConsultReplyRepo::save($data);
        return back();
    }
}
